<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\AppService;
use App\Services\PuvService;
use App\UidAppidRel;
use App\UserApp;
use Carbon\Carbon;
use Illuminate\Http\Request;

class AppController extends Controller
{
    /**
     * @Desc: 配置列表
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function appList(Request $request)
    {
        $wd = $request->input('wd');


        $model = new UserApp();
        if (!empty($wd)) {
            $model = $model->where("name", "like", "%$wd%");
        }

        if (!session('admin')->hasSuperRole()) {
            $loginUid = session('admin')->getUid();

            $appId = UidAppidRel::where("user_id", $loginUid)->pluck("app_id");

            $model = $model->whereIn("id", $appId);
        }

        $list = $model->with("adminUser")->where("del_flag", 0)->paginate(10);
        return view('admin.app.index', ['list' => $list, 'wd' => $wd]);
    }


    /**
     * @Desc: 添加应用
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function appAddView(Request $request)
    {
        return view('admin.app.add');
    }

    /**
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return mixed
     */
    public function appAdd(Request $request)
    {
        $data   = $request->post();
        //$config = new UserApp();

        $userIds = [];
        if (!empty($data['user_id'])) {
            if (strpos($data['user_id'], ",") !== false) {
                $userIds = explode(",", $data['user_id']);
                unset($data["user_id"]);

                $data["user_id"] = !empty($userIds[0]) ? $userIds[0] : 0;
            } else {
                $userIds[] = $data["user_id"];
            }
        }

        $userApp = new UserApp($data);
        $userApp->save();

        $appId = $userApp->id;


        if (!empty($userIds)) {
            $insertParams = [];
            foreach ($userIds as $userId) {
                $tmp = [];
                $tmp["user_id"] = intval($userId);
                $tmp["app_id"] = $appId;
                $tmp["updated_at"] = date("Y-m-d H:i:s");
                $tmp["created_at"] = date("Y-m-d H:i:s");
                $insertParams[] = $tmp;
            }
            UidAppidRel::insert($insertParams);
        }

        return $this->json(200, '添加成功');
    }

    /**
     * @Desc: 修改应用信息
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @param $id
     * @return \Illuminate\View\View
     */
    public function appUpdateView(Request $request, $id)
    {

        $config = UserApp::findOrFail($id);

        $userIds = UidAppidRel::where("app_id", $id)->pluck("user_id")->toArray();

        $config->user_id = implode(",", $userIds);

        return view('admin.app.update', ['config' => $config]);
    }

    public function appUpdate(Request $request, $id)
    {
        $config = UserApp::findOrFail($id);
        $data   = $request->post();

        if (!empty($data['user_id'])) {
            UidAppidRel::where("app_id", $id)->delete();

            if (strpos($data['user_id'], ",") !== false) {
                $userIds = explode(",", $data['user_id']);
                unset($data["user_id"]);
                $data["user_id"] = !empty($userIds[0]) ? $userIds[0] : 0;

                $insertParam = [];
                foreach ($userIds as $userId) {
                    $tmp = [];
                    $tmp["user_id"] = intval($userId);
                    $tmp["app_id"] = $id;
                    $tmp["updated_at"] = date("Y-m-d H:i:s");
                    $tmp["created_at"] = date("Y-m-d H:i:s");
                    $insertParam[] = $tmp;
                }
                UidAppidRel::insert($insertParam);
            } else {
                $upParam["user_id"] = intval($data['user_id']);
                $upParam["app_id"] = $id;
                $upParam["updated_at"] = date("Y-m-d H:i:s");
                $upParam["created_at"] = date("Y-m-d H:i:s");
                UidAppidRel::insert($upParam);
            }
        }

        $config->fill($data);
        $config->save();
        return $this->json(200, '修改成功');
    }

    /**
     * @Desc: 删除应用
     * @Author: hank <maokeyang1031@gmail.com>
     * @param $id
     * @return mixed
     */
    public function appDel($id)
    {
        UserApp::findOrFail($id)->update(["del_flag" => 1]);
        return $this->json(200, '删除成功');
    }


    /**
     *
     * 获取口子数据
     *
     */
    public function getKouZiResult()
    {
        $appId = request("app_id", 0);
        $service = new AppService();
        $list = $service->getKouZiResult($appId);

        return view('admin.app.kouzi', ['list' => $list]);
    }


    /**
     * 显示puv
     * @param $id
     * @return mixed
     */
    public function puv($id)
    {
        $service = new PuvService();
        $yesDate = Carbon::yesterday()->toDateString();
        $today = Carbon::now()->toDateString();

        $data['date0'] = $service->getPuvByAppId($id, $yesDate);
        $data['date1'] = $service->getPuvByAppId($id, $today);

        return view("admin.app.puv", $data);
    }
}
